package com.KMGSoftworks.ShoppingList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ViewListItems extends ListActivity {
	private final String TAG = "ViewListItems";
	private int list_id;
	private String name;
	private int list_count;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewlistitems);
		
		Bundle extras = getIntent().getExtras();
		
		list_id = extras.getInt(Constants.ListID);
		name = extras.getString(Constants.ListName);
		list_count = extras.getInt(Constants.ListCount);
		
		TextView tv = (TextView)findViewById(R.id.txtName);
		if(tv != null){
			tv.setText(name + " List Items");
			
		}
		ListView lv = getListView();
	    lv.setOnCreateContextMenuListener(this);
	    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	        	public void onItemClick(AdapterView<?> parent, View v, int position, long id){
	        		showListItems(position, id);
	        	}   	
			});

		
	    getData();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_listitem, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        	case R.id.new_listitem:
        		createListItem();
        		return true;
        	default:
        		return super.onOptionsItemSelected(item);
        }
    }

	@Override
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    	if(v.getId() == android.R.id.list){
    		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    		CharSequence title = ((TextView) info.targetView.findViewById(R.id.txtListItemName)).getText();
    		menu.setHeaderTitle(title);
    		MenuInflater mi = getMenuInflater();
    		mi.inflate(R.menu.menu_listitem_context, menu);
    	}
    }
	
	@Override
	public boolean onContextItemSelected(MenuItem item){
    	final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	switch(item.getItemId()) {
    		case R.id.deleteListItem:
    			AlertDialog alert = new AlertDialog.Builder(this).create();
    			alert.setTitle("Delete List Item");
    			alert.setMessage("Are you sure?");
    			alert.setButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						Cursor c = (Cursor)getListView().getAdapter().getItem(info.position);
						deleteData(c.getInt(ShoppingList.ListItem.COLUMN_INDEX_ID));
					}
				});
    			alert.setButton2("Cancel", new DialogInterface.OnClickListener() {
    			      public void onClick(DialogInterface dialog, int which) {
    			        return;
    			    }}); 

    			alert.show();
		  		break;
		  	case R.id.editListItem:
		  		Cursor c = (Cursor)getListView().getAdapter().getItem(info.position);
		  		Intent intent = new Intent(Constants.CREATE_LIST_ITEMS_ACTIVITY);
		  	   	intent.putExtra(Constants.ListID, list_id);
		    	intent.putExtra(Constants.ListName, name);
		  		intent.putExtra(ShoppingList.ListItem.COLUMN_NAME_COUNT, c.getString(ShoppingList.ListItem.COLUMN_INDEX_COUNT));
		  		intent.putExtra(Constants.MODE, Constants.MODE_EDIT);
		  		startActivity(intent);
		  		break;
		  	default:
		  		return super.onContextItemSelected(item);
	  }
   	  return true;
   	}

    private void deleteData(int row_id){
	   	Uri listItemUri = ContentUris.withAppendedId(ShoppingList.ListItem.CONTENT_ID_URI_PATTERN, row_id);
    	Log.i(TAG, "delete - " + listItemUri.toString());
    	getContentResolver().delete(listItemUri, null, null);
    } 
	
	private void createListItem() {
    	Intent intent = new Intent(Constants.CREATE_LIST_ITEMS_ACTIVITY);
 	   	intent.putExtra(Constants.ListID, list_id);
    	intent.putExtra(Constants.ListName, name);
    	intent.putExtra(Constants.MODE, Constants.MODE_NEW);
    	startActivity(intent);
	
	}

	private void showListItems(int position, long id){
    	ListView lv = getListView();
    	Cursor c = (Cursor)lv.getItemAtPosition(position);
    	
    	Intent intent = new Intent(Constants.CREATE_LIST_ITEMS_ACTIVITY); 
  	   	intent.putExtra(Constants.ListID, list_id);
    	intent.putExtra(Constants.ListName, name);
  		intent.putExtra(Constants.ListItemCount, c.getInt(ShoppingList.ListItem.COLUMN_INDEX_COUNT));
  		intent.putExtra(Constants.MODE, Constants.MODE_EDIT);
    	
    	startActivity(intent);
    }    

	private void getData() {
 /*
		Cursor c = getContentResolver().query(
    			ShoppingList.ListItem.CONTENT_URI, 
    			null, 
    			null, 
    			null, 
    			null);
    	
    	startManagingCursor(c);

    	int[] viewIDs = new int[] { R.id.txtItemID, R.id.txtListIDForItem,  R.id.txtListItemName, R.id.txtListItemCount };
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
        		this,
        		R.layout.listitem_row,
        		c,
        		ShoppingList.ListItem.ITEM_PROJECTION,
        		viewIDs);
        
        setListAdapter(adapter);
	
        if(c.getCount()==0){
        	createListItem();
        }
*/
	}
	
}
